//
//  NSBundle+Extension.m
//  Pods
//
//  Created by LiuYihua on 2017/4/24.
//
//

#import "NSBundle+Extension.h"

@implementation NSBundle (Extension)
#pragma mark -  获取当前文件或者xib的所在的 Bundle
+ (NSBundle *)getFileBundleWithClass:(Class)aClass
{
    static NSBundle * baseBundle = nil;
    
    baseBundle = [NSBundle bundleForClass:aClass];
    
    return baseBundle;
}
#pragma mark -  获取当前图片资源的所在的Bundle
+ (NSBundle *)getBundleWithClass:(Class)aClass resource:(NSString *)resource
{
    static NSBundle * baseBundle = nil;
    
        
    baseBundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:aClass] pathForResource:resource ofType:@"bundle"]];
    
    return baseBundle;
}

#pragma mark - 获取bundle中的图片资源
+ (UIImage *)getImageNamed:(NSString *)name class:(Class)aClass resource:(NSString *)resource{
    CGFloat scale = [[UIScreen mainScreen] scale];
    name = 3.0 == scale ? [NSString stringWithFormat:@"%@@3x.png", name] : [NSString stringWithFormat:@"%@@2x.png", name];
    UIImage *image = [UIImage imageWithContentsOfFile:[[[NSBundle getBundleWithClass:aClass resource:resource] resourcePath] stringByAppendingPathComponent:name]];
    return image;
}

#pragma makr - 文字国际化配置
+ (NSString *)get_localizedStringForKey:(NSString *)key class:(Class)aClass resource:(NSString *)resource;
{
    return [self get_localizedStringForKey:key value:nil class:aClass resource:resource];
}

+ (NSString *)get_localizedStringForKey:(NSString *)key value:(NSString *)value class:(Class)aClass resource:(NSString *)resource
{
    static NSBundle *bundle = nil;
    if (nil == bundle) {
        NSString *language = [NSLocale preferredLanguages].firstObject;
        if ([language hasPrefix:@"en"]) language = @"en";
        else if ([language hasPrefix:@"zh"]) {
            if ([language rangeOfString:@"Hans"].location != NSNotFound) {
                language = @"zh-Hans";
            } else {
                language = @"zh-Hant";
            }
        } else {
            language = @"en";
        }
        
        bundle = [NSBundle bundleWithPath:[[self getBundleWithClass:aClass resource:resource] pathForResource:language ofType:@"lproj"]];
    }
    value = [bundle localizedStringForKey:key value:value table:nil];
    return [[NSBundle mainBundle] localizedStringForKey:key value:value table:nil];
    
    
}

@end
